package in.muton.edushare.pages.cp.courseFBCP;

import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.panels.paginator.EdusharePageNavigator;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.CoursesDAO;
import com.edushare.common.dao.PollsDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Poll;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class ListCoursePolls extends EdusharePanel {
	private CayenneList<Poll> pollList = null;;
	CourseFeedbackPage page = null;
	IModel<Universities> univModel;

	public ListCoursePolls(String id, IModel<Universities> model,
			final CourseFeedbackPage page) {
		super(id, model, page);
		this.page = page;
		this.univModel = model;
		pollList = initializeListOfPolls(getLoggedInUser(), getORMInterface(),
				univModel);
		Form<Void> f = new Form<Void>("form") {
			@Override
			public void onSubmit() {
				ListCoursePolls.this.page.setActivePollModel(null);
				ListCoursePolls.this.page
						.setActivePanel(CourseFeedbackPage.SHOW_POLL_EDIT);
			}

			private static final long serialVersionUID = -8289965880261486276L;
		};
		add(f);

		PageableListView<IModel<Poll>> plv = new PageableListView<IModel<Poll>>(
				"pollListLV", pollList, 20) {
			@Override
			protected void populateItem(final ListItem<IModel<Poll>> arg0) {
				Link<Void> l;
				arg0.add(l = new Link<Void>("pollSubLink") {
					private static final long serialVersionUID = -1797741553168898994L;

					@Override
					public void onClick() {
						ListCoursePolls.this.page.setActivePollModel(arg0
								.getModelObject());
						if(arg0.getModelObject().getObject().getStatus() == Poll.POLL_STATUS_DRAFT){
							ListCoursePolls.this.page
								.setActivePanel(CourseFeedbackPage.SHOW_POLL_EDIT);
						}else{
							ListCoursePolls.this.page
								.setActivePanel(CourseFeedbackPage.SHOW_POLL_SUBLIST);
						}
					}
				});
				Poll p = arg0.getModelObject().getObject();
				l.add(new Label("pollName", arg0.getModelObject().getObject()
						.getPollName()));
				int count = p.getChildrenPolls().size() + 1; 
				arg0.add(new Label("subPollCount", count+""));
				arg0.add(new Link<Void>("settings"){
					private static final long serialVersionUID = -4508171296858958012L;
					@Override
					public void onClick(){
						page.setActivePollModel(arg0.getModelObject());
						page.setParent(true);
						page.setActivePanel(CourseFeedbackPage.SHOW_POLL_SETTINGS);
					}
				});
				arg0.add(new Label("status", p.getStatusString()));
			}
			private static final long serialVersionUID = 1043847037613237580L;
		};
		add(plv);
		add(new EdusharePageNavigator("navigator", plv));
	}

	/*
	 * For this function, get all drafts for the user, then get all course poll
	 * non-child polls. Arrange as drafts first, then in most-recent poll first.
	 */

	public static CayenneList<Poll> initializeListOfPolls(Users u,
			ORMInterface orm, IModel<Universities> univModel) {
		List<Poll> finalPollList = new ArrayList<Poll>();
		List<Poll> pList = u.getCreatedPolls();
		for (Poll p : pList) {
			if (p.getStatus() == Poll.POLL_STATUS_DRAFT)
				finalPollList.add(p);
		}

		List<Courses> courseList = new CoursesDAO(orm).getUnivCourses(univModel
				.getObject());
		for (Courses c : courseList) {
			List<CourseInstances> cInstList = c.getCourseInstances();
			for (CourseInstances cInst : cInstList) {
				List<Poll> coursePollList = new PollsDAO(orm)
						.getPollsByCreator(Poll.POLL_COURSE, cInst
								.getCourseInstanceId());
				for (Poll p : coursePollList) {
					if (!finalPollList.contains(p) && p.getParentPoll() == null) {
						finalPollList.add(p);
					}
				}
			}
		}
		return new CayenneList<Poll>(finalPollList);
	}

	private static final long serialVersionUID = -1600381801827512594L;
}
